package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.entity.Bing;
import com.bw.entity.Dict;
import com.bw.entity.Zxt;
import com.bw.mapper.TuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class TuController {

    @Autowired
    TuMapper tuMapper;

    @RequestMapping("/tu.do")
    public String tu(Model model){

       List<Dict> levelList =  tuMapper.listLevel();
        model.addAttribute("levelList",levelList);


        return "tu";
    }
    @RequestMapping("/zxt.do")
    @ResponseBody
    public List<Zxt> zxt(Integer levelId, String remark){

        return  tuMapper.zxt(levelId,remark);
    }
    @RequestMapping("/bing.do")
    @ResponseBody
    public List<Bing> bing(Integer levelId, String remark){

        return  tuMapper.bing(levelId,remark);
    }


    @RequestMapping("/excel.do")
    public void excel(Integer levelId, String remark, HttpServletResponse response) throws IOException {

        List<Zxt> list = tuMapper.zxt(levelId, remark);


        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("职位导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Zxt.class).sheet("模板").doWrite(list);
    }
}
